﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using StockMonitor.Configuration;
using CarolLib.FTP;
using CarolLib;

namespace StockMonitor.Business
{
    /// <summary>
    /// 从FTP目录获取【按天的】历史价格 提供程序
    /// </summary>
    /// <remarks>
    /// 代码编写：<author>LANCEZHANG-PC\zhangxl</author><br/>
    /// 编写时间：<datetime>2012-10-16 4:51 PM</datetime>
    /// </remarks>
    public class FtpHistoryPriceProvider : IHistoryPriceProvider
    {
        /// <summary>
        /// 获取某只股票历史价格
        /// </summary>
        /// <param name="stockCode">股票代码</param>
        /// <param name="dayCount">获取从昨天起的天数</param>
        /// <returns>
        /// 历史价格列表
        /// </returns>
        /// <remarks>
        /// 代码编写：<author>LANCEZHANG-PC\zhangxl</author><br/>
        /// 编写时间：<datetime>2012-10-16 4:52 PM</datetime>
        /// </remarks>
        public List<StockPrice> GetHistoryPrice(string stockCode, int dayCount)
        {
            List<StockPrice> list = new List<StockPrice>();

            string path = Application.StartupPath + AppSettings.GetValue<string>("HistoryPricePath") + stockCode + ".txt";
            FileInfo file = new FileInfo(path);

            //确保文件存在且为今天刚更新的
            if (!(file.Exists && file.LastWriteTime > DateTime.Today))
                DownloadHistoryPriceFile(stockCode, path);

            if (file.Exists)
                File.ReadAllLines(path).ToList().ForEach(l => list.Add(new StockPrice() { Date = Convert.ToDateTime(l.Split(',')[0]), Price = Convert.ToDouble(l.Split(',')[1]) }));

            return list;
        }

        /// <summary>
        /// 获取某只股票历史收盘价格
        /// </summary>
        /// <param name="stockCode">股票代码</param>
        /// <param name="dayCount">获取从昨天起的天数</param>
        /// <returns>
        /// 历史收盘价格从远到进排序
        /// </returns>
        /// <remarks>
        /// 代码编写：<author>LANCEZHANG-PC\zhangxl</author><br/>
        /// 编写时间：<datetime>2012-10-16 4:52 PM</datetime>
        /// </remarks>
        public List<double> GetHistoryClosePrice(string stockCode, int dayCount)
        {
            List<double> list = new List<double>();

            string path = Application.StartupPath + AppSettings.GetValue<string>("HistoryPricePath") + stockCode + ".txt";
            FileInfo file = new FileInfo(path);

            bool checkFlag = true;

            //确保文件存在且为今天刚更新的
            if (!(file.Exists && file.LastWriteTime > DateTime.Today))
                checkFlag = DownloadHistoryPriceFile(stockCode, path);

            if (checkFlag)
                File.ReadAllLines(path).ToList().ForEach(l => list.Add(Convert.ToDouble(l.Split(',')[1])));

            return list;
        }

        /// <summary>
        /// 下载历史数据文件.
        /// </summary>
        /// <param name="stockCode">The stock code.</param>
        /// <param name="path">The path.</param>
        /// <returns></returns>
        /// <remarks>
        /// 代码编写：<author>LANCEZHANG-PC\zhangxl</author><br/>
        /// 编写时间：<datetime>2012-10-16 4:52 PM</datetime>
        /// </remarks>
        private static bool DownloadHistoryPriceFile(string stockCode, string path)
        {
            FTPclient ftp = new FTPclient(AppSettings.GetValue<string>("FTPHostName"), false, AppSettings.GetValue<string>("FTPUser"), AppSettings.GetValue<string>("FTPPassword"));

            try
            {
                ftp.Download("\\" + stockCode + ".txt", path, true);
                return true;
            }
            catch (Exception e)
            {
                Log.Exception("历史数据下载失败", e);
                return false;
            }
        }
    }
}
